/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui;

import java.awt.Color;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import net.worlds.SettingDef;
import net.worlds.Settings;
import net.worlds.Util;
import net.worlds.component.JColorPicker;


public class SettingsTableCellComponent {
    public static Component getComponentFor(SettingsTableCellEditor e, int column, String name, Object value)
    {
        Component c;
        {
            SettingDef def;
            def = Settings.getSettingDef(name);
            if(def == null)
                return new JTextField();
            if(column == 0)
            {
                c = new JTextField();
               ((JTextField)c).setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
               ((JTextField)c).setText(value.toString());
            }
            else
            {
                Class propType = def.valueClass;
                if(propType == Boolean.class)
                {
                    if(value == null)
                        value = false;
                    c = new JCheckBox();
                    c.setBackground(java.awt.Color.white);
                    ((JCheckBox)c).setSelected((Boolean) value);
                }
                else if(propType == Color.class)
                {
                    if(value == null)
                        value = new Color(0, 0, 0);
                    c = new JColorPicker(e, (Color) value);
                }
                else
                {
                    if(value == null)
                        value = "";
                    c = new JTextField();
                    ((JTextField)c).setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
                    if(!(value instanceof Float))
                        ((JTextField)c).setText(value.toString());
                    else
                    {
                        double d = Float.parseFloat(value.toString());
                        ((JTextField)c).setText(Double.toString(Util.roundTwoDecimals(d)));
                    }
                }
            }
        }
        return c;
    }
}
